﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace G.Controls
{
    public class CellRowItem : ListBoxItem
    {
        public static readonly DependencyProperty ArrayItemsProperty = DependencyProperty.Register("ArrayItems", typeof(Array), typeof(CellRowItem), new PropertyMetadata(null, ArrayItemsPropertyChanged));

        private static void ArrayItemsPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            CellRowItem data = d as CellRowItem;
            data.CreateItmes();
        }

        public Array ArrayItems
        {
            get
            {
                return this.GetValue(ArrayItemsProperty) as Array;
            }
            set
            {
                this.SetValue(ArrayItemsProperty, value);
            }
        }

        internal GridListBox HostListBox;

        public static readonly DependencyProperty OrientationProperty = DependencyProperty.Register("Orientation", typeof(Orientation), typeof(CellRowItem), new PropertyMetadata(Orientation.Horizontal));

        public Orientation Orientation
        {
            get
            {
                return (Orientation)this.GetValue(OrientationProperty);
            }
            set
            {
                this.SetValue(OrientationProperty, value);
            }
        }

        private void CreateItmes()
        {
            StackPanel stack = new StackPanel() { Orientation = this.Orientation };
            Array data = this.ArrayItems;
            for (int i = 0; i < data.Length; i++)
            {
                CellItem item = new CellItem();
                item.RowItem = this;
                item.Content = HostListBox.CellContentTemplate.LoadContent();
                item.DataContext = data.GetValue(i);
                stack.Children.Add(item);
            }
            this.Content = stack;
        }

        internal void SelectItem(object cellItem, object item)
        {
            this.HostListBox.ItemSelected(cellItem,item);
        }
    }
}
